import { Component, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';
import { InitService } from '../../../public/serve/init/init.service';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { keyword } from '../../keyword.data';
import { appKeywordUrl } from '../../keyword.url';
import { DatePipe } from '@angular/common';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { ActivatedRoute } from '@angular/router';
import { Service } from 'app/server/server';
import {appSynonymUrl} from '../../../synonym/synonym.url';

declare var $:any;
@Component({
  selector: 'app-trial-add',
  templateUrl: './trial-add.component.html'
})
export class TrialAddComponent implements OnInit {

  @Output() onSubmit = new EventEmitter<void>();
  @ViewChild('addForm') formElement: HTMLFormElement;
  @Input() themeTagListVal: any;

  checkStatus : number = 0; //默认是未审核
  keywordListVal : Array<object>;
  details: object = {};

  showAddChild: any = 0;

  constructor(
    private server: Service,
    public initService: InitService,
    private request: CudTableService,
    private hintsService: HintsService,
    private datePipe: DatePipe,
    private removeTableService: RemoveTableService,
    private _activatedRoute: ActivatedRoute
  ) { }

  ngOnInit() {
    this.server.onParentKeyworkTabListEmit.subscribe((value: any) => {
      this._list();
    });
  }

  /**
   * 获取上级主题词列表
   */
 _list(){
    let data = this._tableData();
    this.request.postRequest(this.server.baseUrl +appKeywordUrl.getThemeWordsList,data).then((res:any) => {
      if (res.result_code === '0000') {
        this.keywordListVal = res.result;
      }else{
        this.hintsService.messages(res.message,res.result_code);
      }
      return;
    })
    .catch((err: any)=>{
      this.hintsService.messages("error","error");
      return;
    });
  }
  
   /**
   * 主题词详情
   * @param info
   */
  _details(info: object){
    this.details = info;
    this.showAddChild = 1;
  }

  /**
   * 新增提交
   * @param info
   */
  _onSubmit(info:any){

    info.token = keyword.retrieval.token;
    info.createBy = this.server.getuserName();
    info.createTime = this.datePipe.transform(new Date(), 'yyyy-MM-dd HH:mm:ss');
    info.checkStatus = keyword.retrieval.checkStatus;
    //info.checkStatus = this.checkStatus;//新增默认是未审核
    info.child = 0;
    info.themeTableId = keyword.retrieval.themeTableId;
    info.themeGroupId = keyword.retrieval.themeGroupId;

    //点击列表中的‘新增子主题词’
    if(this.showAddChild === 1){
      info.parentId = this.details['themeWordsId'];
    }

    this.request
    .postRequest(this.server.baseUrl +appKeywordUrl.addThemeWords,info)
    .then(res =>{
      if(res.result_code === "0000"){
        this.onSubmit.emit();
      }
      this._initAdd();
      this._showAddChildInfo();
      this.hintsService.messages(res.message,res.result_code);
      return;
    })
    .catch(err =>{
      this._initAdd();
      this._showAddChildInfo();
      this.hintsService.messages("error","error");
      return;
    });
  }

   /**
    * 初始化
    */
   private _initAdd(){
    this.initService._init();
    this.formElement.reset();
    $("#add").modal("hide");
   }

   //查询参数
  private _tableData(){
    return {
      token         : keyword.retrieval.token,
      pageNo        : keyword.pagination.pageNo,
      pageSize      : keyword.pagination.pageSize,
      status        : keyword.retrieval.status,
      themeCode     : keyword.retrieval.themeCode,
      checkStatus   : keyword.retrieval.checkStatus,
      startTime     : keyword.retrieval.startTime,
      endTime       : keyword.retrieval.endTime,
      queryCriteria : keyword.retrieval.queryCriteria,
      themeTableId  : keyword.retrieval.themeTableId
    }
  }

  _showAddChildInfo(){
    this.showAddChild = 0;
  }

}
